////
//
/// \file   ./src/dereferenced_name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/dereferenced_name_node.hpp>

#include <cassert>
#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/internal_name_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief  Parse an internal name node
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This builds an internal name parse node for use within nested name
/// resolution chains. It parses out either an explicit label for the name or
/// enforces grouping parentheses before handing off the parse to a compound
/// name construct.

bool
DereferencedNameNode::parse( const iterator &first,
                             const iterator &last,
                             iterator &last_parsed )
{
  assert( get_text().empty() && !node_ptr
          && "This node was parsed more than once!" );

  if( *first == '@' )
  {
    boost::scoped_ptr<ParseTreeNode> internal_name( new InternalNameNode );
    if( internal_name->parse( first + 1, last, last_parsed ) )
    {
      set_text( "@" + internal_name->get_text() );
      node_ptr.swap( internal_name );
      return true;
    }
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

} //  end parser namespace
} //  end inc namespace
